package com.cskaoyan.app.wxcontroller;

import com.cskaoyan.app.bean.bo.CaptchaBO;
import com.cskaoyan.app.bean.bo.LoginBO;
import com.cskaoyan.app.bean.bo.WxRegisBO;
import com.cskaoyan.app.bean.bo.WxResetBO;
import com.cskaoyan.app.bean.common.BaseRespVo;
import com.cskaoyan.app.bean.po.MarketUser;
import com.cskaoyan.app.bean.vo.wx.WxAuthUserInfo;
import com.cskaoyan.app.bean.vo.wx.WxLoginDataVO;
import com.cskaoyan.app.exception.ValidationException;
import com.cskaoyan.app.shiro.CustomAuthToken;
import com.cskaoyan.app.shiro.LoginType;
import com.cskaoyan.app.util.EncryptUtil;
import com.cskaoyan.app.util.StringsUtil;
import com.cskaoyan.app.util.ValidationUtil;
import com.cskaoyan.app.wxservice.WxAuthService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("wx/auth")
public class WxAuthController {

    @Autowired
    private WxAuthService authService;

    @PostMapping("login")
    public BaseRespVo<Object> login(@RequestBody @Validated LoginBO loginBO, BindingResult bindingResult)
            throws ValidationException {
        ValidationUtil.valid(bindingResult);
        String username = loginBO.getUsername();
        String password = loginBO.getPassword();
        String encrypt = EncryptUtil.encrypt(password);
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(new CustomAuthToken(username, encrypt, LoginType.WX_PASSWD));
        } catch (AuthenticationException e) {
            return BaseRespVo.unAuthed();
        }
        MarketUser user = authService.getWxUserByName(username);
        Session session = subject.getSession();
        //将用户id保存到会话中
        session.setAttribute("userid", user.getId());
        String nick = StringsUtil.isEmpty(user.getNickname()) ? user.getNickname() : user.getUsername();
        WxAuthUserInfo userInfo = new WxAuthUserInfo(nick, user.getAvatar());
        WxLoginDataVO dataVO = new WxLoginDataVO((String) subject.getSession().getId(), userInfo);
        return BaseRespVo.ok(dataVO);
    }

    @PostMapping("logout")
    public BaseRespVo<Object> logout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return BaseRespVo.ok();
    }

    @PostMapping("regCaptcha")
    public BaseRespVo<Object> captcha(@RequestBody @Validated CaptchaBO captchaBO,
          BindingResult bindingResult) throws ValidationException {
        ValidationUtil.valid(bindingResult);
        String mobile = captchaBO.getMobile();
        authService.sendCaptcha(mobile, 6);
        return BaseRespVo.ok();
    }

    @PostMapping("register")
    public BaseRespVo<WxLoginDataVO> register(@RequestBody @Validated WxRegisBO regisBO,
          BindingResult bindingResult) throws ValidationException {
        ValidationUtil.valid(bindingResult);
        WxLoginDataVO data = authService.register(regisBO);
        return BaseRespVo.ok(data);
    }

    @PostMapping("reset")
    public BaseRespVo<Object> reset(@RequestBody @Validated WxResetBO resetBO,
        BindingResult bindingResult) throws ValidationException {
        ValidationUtil.valid(bindingResult);
        authService.restPass(resetBO);
        return BaseRespVo.ok();
    }
}
